package handlers

import (
	"GIN-API/casbin"
	"github.com/gin-gonic/gin"
	"net/http"
)

// AddPolicy 添加 Casbin 策略
func AddPolicy(c *gin.Context) {
	var policy []string
	if err := c.ShouldBindJSON(&policy); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
		return
	}

	enforcer := casbin.GetEnforcer()
	ok, err := enforcer.AddPolicy(policy)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "添加策略失败"})
		return
	}
	if !ok {
		c.JSON(http.StatusBadRequest, gin.H{"error": "策略已存在"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "策略添加成功"})
}
